package com.zy.mc_module.viewmodel;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zy.mc_module.R;

import java.util.HashMap;
import java.util.Map;

/**
 * @Date 19/3/21
 * @Author dq
 * 商品筛选
 */
public class FilterViewModel {
    private Context context;
    private LinearLayout filter;
    private TextView tv_All, tv_Auth, tv_UnAuth;
    private ImageView iv_upAll, iv_downAll, iv_upAuth, iv_downAuth, iv_upUnAuth, iv_downUnAuth;
    private RelativeLayout relativeUnAuthentic, relativeAuthentic, relativeAll;

    private onFilterListener filterListener;
    private Map<String, Object> map;

    public FilterViewModel(Context context, LinearLayout filter, boolean isShowSales) {
        this.context = context;
        this.filter = filter;
        initView();
    }

    public FilterViewModel(Context context, LinearLayout filter, boolean isShowSales, boolean isIntegral) {
        this.context = context;
        this.filter = filter;
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_filter, null, false);
        relativeAll = view.findViewById(R.id.filter_all);
        relativeAuthentic = view.findViewById(R.id.filter_authentic);
        relativeUnAuthentic = view.findViewById(R.id.filter_un_authentic);
        tv_All = view.findViewById(R.id.filter_all_text);
        tv_Auth = view.findViewById(R.id.filter_authentic_text);
        tv_UnAuth = view.findViewById(R.id.filter_un_authentic_text);
        iv_upAll = view.findViewById(R.id.filter_all_up);
        iv_downAll = view.findViewById(R.id.filter_all_down);
        iv_upAuth = view.findViewById(R.id.filter_authentic_up);
        iv_downAuth = view.findViewById(R.id.filter_authentic_down);
        iv_upUnAuth = view.findViewById(R.id.filter_un_authentic_up);
        iv_downUnAuth = view.findViewById(R.id.filter_un_authentic_down);
        filter.removeAllViews();
        filter.addView(view);

        //综合
        relativeAll.setOnClickListener(v -> {
            sortType("", "");
            clearState();
            tv_All.setTextColor(context.getResources().getColor(R.color.colorPrimary));
            iv_downAll.setColorFilter(context.getResources().getColor(R.color.colorPrimary));
        });

        //新品
        relativeAuthentic.setOnClickListener(v -> {
            sortType("new_goods", "false");
            clearState();
            tv_Auth.setTextColor(context.getResources().getColor(R.color.colorPrimary));
            iv_downAuth.setColorFilter(context.getResources().getColor(R.color.colorPrimary));
        });

        //价格
        relativeUnAuthentic.setOnClickListener(v -> {
            clearState();
            tv_UnAuth.setTextColor(context.getResources().getColor(R.color.colorPrimary));
            iv_downUnAuth.setColorFilter(context.getResources().getColor(R.color.colorPrimary));
        });
    }


    private void clearState() {
        tv_All.setTextColor(context.getResources().getColor(R.color.color_666));
        tv_Auth.setTextColor(context.getResources().getColor(R.color.color_666));
        tv_UnAuth.setTextColor(context.getResources().getColor(R.color.color_666));
        iv_upAll.clearColorFilter();
        iv_downAll.clearColorFilter();
        iv_upAuth.clearColorFilter();
        iv_downAuth.clearColorFilter();
        iv_upUnAuth.clearColorFilter();
        iv_downUnAuth.clearColorFilter();
    }

    /***
     * @param type 排序类型
     * @param isAsc 对应的升降
     *
     * 价格排序price  /  新品new_goods
     * true为升序,false为倒序
     */
    private void sortType(String type, String isAsc) {
        if (null != filterListener) {
            map = new HashMap<>();
            map.put("sortType", type);
            map.put("isAsc", isAsc);
            filterListener.onFilter(map);
        }
    }

    public void setOnFilterListener(onFilterListener filterListener) {
        this.filterListener = filterListener;
    }

    /***
     * 选择筛选条件后刷新请求
     */
    public interface onFilterListener {
        /***
         * 搜索条件选择完成后,刷新数据
         * @param map
         */
        void onFilter(Map<String, Object> map);
    }
}
